import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:jetour_line/base_widget/base_scaffold.dart';
import 'package:jetour_line/base_widget/loading_widget.dart';
import 'package:jetour_line/model/line_detail_model.dart';
import 'package:jetour_line/network/aha_http.dart';
import 'package:jetour_line/pages/baseWidget/line_item_widget.dart';
import 'package:jetour_line/pages/common/common_list_head_widget.dart';
import "package:jetour_line/utils/extension_int.dart";
import "package:jetour_line/models/line_list_model.dart" as line_list;
import "package:jetour_line/base_widget/common_scaffold.dart";
import "package:jetour_line/base_widget/single_line_input_widget.dart";

import '../../base_widget/base_image.dart';
import '../../base_widget/my_refresh_view.dart';
import '../../model/city_list_model.dart';
import '../../my_colors.dart';
import '../../utils/page_push_utils.dart';
import '../home/city_choose_page.dart';

class LineListPage extends StatefulWidget {
  int? cityId;

  String? cityName;

  LineListPage({Key? key, this.cityName, this.cityId}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _LineListPage();
  }
}

class _LineListPage extends State<LineListPage> {
  List<line_list.ListElement>? lineList;

  final MyRefreshViewController _controller = MyRefreshViewController();

  int page = 1;
  String? keyWord;
  String city = '';
  int cityid = 0;
  int theme = 0;

  @override
  void initState() {

    super.initState();
    if (widget.cityId! > 0) {
      cityid = widget.cityId!;
      city = widget.cityName!;
    }
    getLineList();
  }

  @override
  void dispose() {
    super.dispose();
  }
  final ScrollController _scrollController = ScrollController();
  @override
  Widget build(BuildContext context) {
    return BaseScaffold(
        title: '精选路书',
        body: (lineList == null || lineList!.isEmpty)
            ? LoadingWidget(type: lineList != null && lineList!.isEmpty ? 1 : 0,errorMsg: "快来分享您的路书吧",)
            : GestureDetector(
                onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
                child: Container(
                    color: const Color(0xfff1f1f1),
                    child: Column(
                      children: [
                        Container(
                          padding: EdgeInsets.only(left: 32.px, right: 32.px),
                          width: double.infinity,
                          height: 100.px,
                          color: MyColors.white,
                          alignment: Alignment.center,
                          child: ClipRRect(
                            borderRadius:
                                BorderRadius.all(Radius.circular(32.px)),
                            child: Container(
                              color: MyColors.colorF1F1F1,
                              height: 64.px,
                              child: Row(
                                children: [
                                  Container(
                                    color: Colors.transparent,
                                    child: Row(
                                      children: [
                                        GestureDetector(
                                          onTap: () {
                                            PagePushUtils.push(
                                                    context, ChooseCityScreen())
                                                .then((value) {
                                              CityModel model =
                                                  value as CityModel;
                                              setState(() {
                                                city = model.name;
                                                cityid = model.id;
                                              });
                                              _controller.beginRef();
                                            });
                                          },
                                          child: Container(
                                            color: Colors.transparent,
                                            child: Row(
                                              children: [
                                                SizedBox(
                                                  height: 64.px,
                                                  width: 26.px,
                                                ),
                                                Text(
                                                  city,
                                                  style: TextStyle(
                                                      fontSize: 28.px,
                                                      color:
                                                          MyColors.color333333),
                                                ),
                                                SizedBox(
                                                  width: 8.px,
                                                ),
                                                BaseImage(
                                                  image: 'icon_meopen',
                                                  width: 28.px,
                                                  height: 28.px,
                                                ),
                                              ],
                                            ),
                                          ),
                                        ),
                                        Container(
                                          margin: EdgeInsets.only(
                                              left: 15.px, right: 15.px),
                                          width: 1.px,
                                          height: 28.px,
                                          color: MyColors.color999999,
                                        ),
                                        BaseImage(
                                          image: 'ls_navbtn_search',
                                          width: 40.px,
                                          height: 40.px,
                                        ),
                                      ],
                                    ),
                                  ),
                                  Expanded(
                                      child: TextField(
                                    onSubmitted: (value) {
                                      keyWord = value;
                                      _controller.beginRef();
                                    },
                                    onChanged: (value) {
                                      keyWord = value;
                                    },
                                    style: TextStyle(
                                        fontSize: 28.px,
                                        color: MyColors.color333333),
                                    decoration: const InputDecoration(
                                      border: InputBorder.none,
                                      hintText: '请输入关键词',
                                      isDense: true,
                                      contentPadding:
                                          EdgeInsets.fromLTRB(8, 0, 0, 0),
                                    ),
                                  )),
                                  SizedBox(
                                    width: 26.px,
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ),
                        CommonListHeadWidget(
                            isLine: true,
                            callBack: (int id) {
                              setState(() {
                                theme = id;
                              });
                              page = 1;
                               getLineList();
                            }),
                        SizedBox(
                          height: 20.px,
                        ),
                        Expanded(
                          child: MyRefreshView(
                            controller: _controller,
                            refreshCallBack: (bool isUp) {
                              if (isUp) {
                                //下拉刷新
                                page = 1;
                              } else {
                                //上划加载更多
                                page++;
                              }

                              getLineList();
                            },
                            child: ListView.builder(
                              controller: _scrollController,
                              itemBuilder: (context, index) {
                                return LineItemWidget(
                                  line: lineList![index],
                                );
                              },
                              itemCount: lineList!.length,
                            ),
                          ),
                        )
                      ],
                    )),
              ));
  }

  void getLineList() {
    Map<String, dynamic>? data = {};
    if (keyWord != null) {
      data['keyword'] = keyWord;
    }
    if (cityid > 0) {
      data['stCity'] = cityid;
    }
    if (theme > 0) {
      data['theme'] = theme;
    }
    AhaHttp()
        .post(AhaRouter.lineList, params: {"page": page}, data: data)
        .then((result) {
      FocusScope.of(context).unfocus();
      lineList ??= [];
      line_list.LineListModel data = line_list.LineListModel.fromJson(result);
      if (page == 1) {
        _scrollController.animateTo(0,
            duration: const Duration(milliseconds: 1), curve: Curves.ease);
        lineList!.clear();
      }
      setState(() {
        lineList!.addAll(data.list);
      });
      _controller.refreshEnd(page: page, maxPage: data.meta.maxPage);
    }).catchError((e) {
      FocusScope.of(context).unfocus();
      _controller.refreshEnd();
    });
  }
}
